import { deepClone } from '@/utils/helper/jsUtil'

function adapterServiceItemData(data) {
  const list = []
  data.inputData?.forEach(item => {
    const paramItem = {
      paramName: item.name,
      id: item.id,
      interfaceBid: item.interfaceBid,
      serviceBid: item.serviceBid,
      optionalFlag: item.optionalFlag,
      paramKey: item.identifier,
      category: item.category,
      allowEdit: item.allowEdit,
      hasRefSignal: item.hasRefSignal,
      memberList: addStructMemberList(item.memberList, item)
    }
    list.push(paramItem)
  })
  return list
}

// 结构体逐级解析到memberList中
function addStructMemberList(list, parent) {
  const res = []
  list.forEach(item => {
    res.push({
      paramName: item.name,
      paramKey: item.identifier,
      category: item.category,
      id: item.id,
      discreteStr: item.discreteStr,
      specs: item.specs,
      memberList: item.memberList ? addStructMemberList(item.memberList, item) : null,
      physicalMin: item.physicalMin ? item.physicalMin - 0 : -Infinity,
      physicalMax: item.physicalMax ? item.physicalMax - 0 : Infinity,
      showParamOpen: false,
      openParam: {
        isOpen: false,
        dataType: item.category,
        description: '',
        enumValue: [],
        maxValue: undefined,
        value: undefined
      },
      calcRelation: (item.category === 'String' || item.category === 'Struct' || item.specs) ? '0' : '1',
      value: item.allowEdit ? null : item.defaultValue,
      hasRefSignal: parent.hasRefSignal
    })
  })
  return res
}

function formatComponentsProp(list, inputData) {
  const props = {}
  list.forEach(item => {
    item.props = {}
    const addProp = (prop) => {
      if (prop.fieldType === 'number') {
        prop.defaultValue = prop.defaultValue - 0
      } else if (prop.fieldType === 'boolean') {
        prop.defaultValue = prop.defaultValue === 'true'
      }
      item.props[prop.fieldName] = prop.defaultValue
    }
    item.paramWidgetProperties?.inputParam.forEach(prop => { addProp(prop) })
    item.paramWidgetProperties?.expand.forEach(prop => { addProp(prop) })
    item.paramWidgetProperties?.paramMapping?.forEach(prop => {
      item.props[prop.outputParam] = prop.serviceParamIdentifier
    })
    item.props.paramName = item.paramName
    item.props.widgetClassName = item.widgetClassName
    const findItem = (list) => {
      list.forEach(item2 => {
        if (`${item2.id}` === item.paramParentId) {
          const param = item2.memberList.find(item3 => item3.identifier === item.paramKey[0])
          if (!props[param.id + param.identifier]) { props[param.id + param.identifier] = item.props } // 有值不覆盖
          if (item.paramKey.length === 2) { // 二维控件
            const param = item2.memberList.find(item3 => item3.identifier === item.paramKey[1])
            props[param.id + param.identifier] = deepClone(item.props)
          }
        } else if (item.paramParentId === '0') { // 非结构体不取memberlist里面的值
          if (item.paramKey[0] === item2.identifier) {
            const param = item2.memberList[0]
            props[param.id + param.identifier] = item.props
          }
        } else if (item2.memberList) {
          findItem(item2.memberList)
        }
      })
    }
    findItem(inputData)
  })
  return props
}

export {
  adapterServiceItemData,
  formatComponentsProp
}
